package com.ht.service.impl;

import com.ht.dao.SysUserDAO;
import com.ht.dao.impl.SysUserDAOImpl;
import com.ht.entity.SysUserEntity;
import com.ht.enums.ErrorCode;
import com.ht.exception.BusinessException;
import com.ht.service.SysUserService;

import java.sql.SQLException;

public class SysUserServiceImpl implements SysUserService {

    private final SysUserDAO sysUserDAO = new SysUserDAOImpl();

    @Override
    public boolean login(String username, String password) throws BusinessException {
        try {
            SysUserEntity entity = sysUserDAO.queryByUsername(username);
            if (entity == null) {
                throw new BusinessException(ErrorCode.USER_IS_NOT_EXISTS);
            }
            //判断用户是否被禁用
            if (entity.getStatus() == 0) {
                throw  new BusinessException(ErrorCode.User_IS_NOT_DISABLED);
            }
            //判断密码是否错误
            if(!entity.getPassword().equals(password)){
                throw new BusinessException(ErrorCode.USER_IS_NOT_INVALID);
            }else{
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
            throw new BusinessException(ErrorCode.DATABASE_ERROR);
        }
    }
}
